package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.EnterpriseWeChat.entity.YCOrder;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetYcOrderAll extends RESTBase {
    private final static String RANGE = "10";//分页数量
    private String ip;
    private String account;
    private String password;

    public GetYcOrderAll(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<YCOrder> getData(String storeId, int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(storeId,page), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<YCOrder> ycOrders= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                YCOrder ycOrder = new YCOrder();
                ycOrder.setDocNo(jsonArray.getJSONArray(i).getString(0));
                ycOrder.setBillDate(jsonArray.getJSONArray(i).getString(1));
                ycOrder.setStoreName(jsonArray.getJSONArray(i).getString(2));
                ycOrder.setType(jsonArray.getJSONArray(i).getString(3));
                ycOrder.setStatus(jsonArray.getJSONArray(i).getString(4));
                ycOrder.setYcOrderId(jsonArray.getJSONArray(i).getString(5));
                ycOrder.setTotAmtActual(jsonArray.getJSONArray(i).getString(6));
                ycOrder.setTotQty(jsonArray.getJSONArray(i).getString(7));
                ycOrder.setIsSelfRaIs(jsonArray.getJSONArray(i).getString(8));
                ycOrders.add(ycOrder);
            }
        }

        return ycOrders;
    }

    private MultiValueMap Data(String storeId,int page) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("DOCNO");
        columns.add("BILLDATE");
        columns.add("'C_STORE_ID;NAME'");
        columns.add("YC_SOTYPE");
        columns.add("STATUS");
        columns.add("ID");
        columns.add("TOT_AMT_ACTUAL");
        columns.add("TOT_QTY");
        columns.add("IS_SELFRAIS");

        params.put("columns", columns);
        params.put("table", "YC_RETAILOUT");//表名
        params.put("range",RANGE);
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));

        HashMap paramss = new HashMap();

        paramss.put("column","'C_STORE_ID'");
        String ac = "'"+storeId+"'";

        paramss.put("condition",ac);


        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetYcOrderAll g = new GetYcOrderAll("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("1515",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
